package api

import (
	ArticleModel "blog/app/model/articles"
	"blog/app/request/article"
	"blog/core/request"

	"github.com/gin-gonic/gin"
)

type ArticleApi struct{}

// GetLists 获得文章列表
func (a *ArticleApi) GetLists(req request.InterfaceRequest) {
	param := &article.Lists{
		Page:  req.InputIntDefault("page", 1),
		Limit: req.InputIntDefault("limit", 10),
		Title: req.InputStr("title"),
	}

	list, count := ServiceGroup.articles.GetList(param)
	req.Response().Count(count).Data(list).Success()
}

// GetInfo 获得文章详情
func (a *ArticleApi) GetInfo(req request.InterfaceRequest) {
	info, err := ServiceGroup.articles.GetInfo(req.ParamInt("id"))
	req.Response().Data(info).WithErr(err)
}

// Create 创建新的文章
func (a *ArticleApi) Create(req request.InterfaceRequest) {
	param := &article.Create{
		Title:      req.InputStr("title"),
		Cid:        req.InputInt("cid"),
		Tid:        req.InputStr("tid"),
		MainImg:    req.InputStr("main_img"),
		Content:    req.InputStr("content"),
		IsComment:  req.InputIntDefault("is_comment", 0),
		IsPublish:  req.InputIntDefault("is_publish", 1),
		IsOriginal: req.InputIntDefault("is_original", 1),
		Type:       req.InputIntDefault("is_type", 0),
	}

	if err := req.Validate().Check(param); err != nil {
		req.Response().Message(err.Error()).Error()
		return
	}

	req.Response().Message("创建成功").WithErr(ServiceGroup.articles.Create(param))
}

// Del , 删除文章
func (a *ArticleApi) Del(req request.InterfaceRequest) {
	id := req.ParamInt("id")
	req.Response().Message("删除成功").WithErr(ServiceGroup.articles.Del(id))
}

// Edit , 编辑文章
func (a ArticleApi) Edit(c *gin.Context) {
	// req := request.Request(c)

	// var dataValidate *ArticleModel.ArticleCreate = &ArticleModel.ArticleCreate{
	// 	Title:      req.InputStr("title"),
	// 	SubTitle:   req.InputStr("subtitle"),
	// 	Cid:        req.InputInt("cid"),
	// 	Tid:        req.InputStr("tid"),
	// 	MainImg:    req.InputStr("main_img"),
	// 	Content:    req.InputStr("content"),
	// 	IsComment:  req.InputIntDefault("is_comment", 0),
	// 	IsPublish:  req.InputIntDefault("is_publish", 1),
	// 	IsOriginal: req.InputIntDefault("is_original", 1),
	// 	Type:       req.InputIntDefault("is_type", 0),
	// 	Desc:       req.InputStr("desc"),
	// }

	// if msg, err := validates.Validate.Article.Validate(dataValidate); err != nil {
	// 	req.Response().Message(msg).Error()
	// 	return
	// }

	// //TODO::对map的验证有问题。。后面再调整
	// var data map[string]interface{} = map[string]interface{}{
	// 	"Title":      req.InputStr("title"),
	// 	"SubTitle":   req.InputStr("subtitle"),
	// 	"Cid":        req.InputInt("cid"),
	// 	"Tid":        req.InputStr("tid"),
	// 	"MainImg":    req.InputStr("main_img"),
	// 	"IsComment":  req.InputIntDefault("is_comment", 0),
	// 	"IsPublish":  req.InputIntDefault("is_publish", 1),
	// 	"IsOriginal": req.InputIntDefault("is_original", 1),
	// 	"Type":       req.InputIntDefault("is_type", 0),
	// 	"Content":    req.InputStr("content"),
	// 	"Desc":       req.InputStr("desc"),
	// }

	// req.Response().Message("修改成功").WithErr(ServiceGroup.articles.Edit(req.ParamInt("id"), data))
}

type ArticleCateApi struct {
}

// GetLists , 获得文章分类列表
func (receiver *ArticleCateApi) GetLists(c *gin.Context) {
	req := request.Request(c)
	lists, err := ServiceGroup.articleCate.GetLists()
	if err != nil {
		req.Response().Message(err.Error()).Error()
		return
	}

	req.Response().Message("查询成功").Data(lists).Success()
}

func (receiver *ArticleCateApi) GetInfo(c *gin.Context) {
	req := request.Request(c)
	info, err := ServiceGroup.articleCate.GetInfo(req.ParamInt("id"))
	if err != nil {
		req.Response().Message(err.Error()).Error()
		return
	}

	req.Response().Message("查询成功").Data(info).Success()
}

func (receiver *ArticleCateApi) Del(c *gin.Context) {
	req := request.Request(c)
	req.Response().Message("删除成功").WithErr(ServiceGroup.articleCate.DelInfo(req.ParamInt("id")))
}

func (receiver *ArticleCateApi) Create(c *gin.Context) {
	req := request.Request(c)
	var param ArticleModel.Cate = ArticleModel.Cate{
		Name: req.InputStr("name"),
	}
	req.Response().Message("添加成功").WithErr(ServiceGroup.articleCate.Create(param))
}

func (receiver *ArticleCateApi) Edit(c *gin.Context) {
	req := request.Request(c)
	var param ArticleModel.Cate = ArticleModel.Cate{
		Name: req.InputStr("name"),
	}
	req.Response().Message("编辑成功").WithErr(ServiceGroup.articleCate.Edit(req.ParamInt("id"), param))
}

type ArticleTagAPi struct {
}

func (receiver *ArticleTagAPi) GetLists(c *gin.Context) {
	req := request.Request(c)
	lists, err := ServiceGroup.articleTag.GetLists()
	if err != nil {
		req.Response().Message(err.Error()).Error()
		return
	}

	req.Response().Message("查询成功").Data(lists).Success()
}

func (receiver *ArticleTagAPi) GetInfo(c *gin.Context) {
	req := request.Request(c)
	req.Response().Message("查询成功").WithErrData(ServiceGroup.articleTag.GetInfo(req.ParamInt("id")))
}

func (receiver *ArticleTagAPi) Create(c *gin.Context) {
	req := request.Request(c)
	param := ArticleModel.Tag{
		Name: req.InputStr("name"),
	}

	req.Response().Message("添加成功").WithErr(ServiceGroup.articleTag.Create(param))
}

func (receiver *ArticleTagAPi) Del(c *gin.Context) {
	req := request.Request(c)
	req.Response().Message("删除成功").WithErr(ServiceGroup.articleTag.Del(req.ParamInt("id")))
}

func (receiver *ArticleTagAPi) Edit(c *gin.Context) {
	req := request.Request(c)
	param := ArticleModel.Tag{
		Name: req.InputStr("name"),
	}
	req.Response().Message("编辑成功").WithErr(ServiceGroup.articleTag.Edit(req.ParamInt("id"), param))
}
